﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Snake.Grid
{
    [CreateAssetMenu]
    public class BuildingType : ScriptableObject
    {
        public string buildingName;
        public Transform buildingVisual;

        public Vector2Int size;

        /// <summary>
        /// 获得建筑占位置
        /// </summary>
        /// <param name="point">世界坐标</param>
        /// <param name="rotation">旋转方向</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public List<Vector2Int> GetPoint(Vector2 point, EBuildingRotation rotation = EBuildingRotation.None)
        {
            //得到旋转后的尺寸
            var rotatedSize = rotation switch
            {
                EBuildingRotation.None => size,
                EBuildingRotation.Turn90 => new Vector2Int(size.y, size.x),
                EBuildingRotation.Turn180 => size,
                EBuildingRotation.Turn270 => new Vector2Int(size.y, size.x),
                _ => throw new ArgumentOutOfRangeException(nameof(rotation), rotation, null)
            };

            var needPoint = new List<Vector2Int>();

            var xCount = rotatedSize.x;
            var xBegin = (xCount - 1) * 0.5f;
            var yCount = rotatedSize.y;
            var yBegin = (yCount - 1) * 0.5f;
            for (var x = 0; x < xCount; x++)
            {
                for (var y = 0; y < yCount; y++)
                {
                    needPoint.Add(new Vector2Int(Mathf.RoundToInt(point.x + x - xBegin),
                                                 Mathf.RoundToInt(point.y + y - yBegin)));
                }
            }

            return needPoint;
        }
    }
}